var exec = require("child_process").exec;
var sleep = require("./sleep");

/*
    关于"Node入门" 所提到的解决回调函数阻塞问题的方法似乎是不正确的。。。
*/
// function index(res){
//     console.log("Request handler 'index' was called.");

//     exec("find /", {timeout : 1000, maxBuffer : 20000*1024}, function(error, stdout, stderr){
//         sleep(10000);

//         res.writeHead("200", {"Content-Type" : "text/html"});
//         res.write("I am INDEX/.");
//         res.end();
//     });
// }

function index(res){
    console.log("Request handler 'index' was called.");

    // setImmediate(function(){
        sleep(5000);

        res.writeHead("200", {"Content-Type" : "text/html"});
        res.write("I am INDEX/*");
        res.end();
    // });

    console.log("Request handler 'index' process end.")
}

function user(res){
    console.log("Request handler 'user' was called.");

    // setImmediate(function(){
        res.writeHead("200", {"Content-Type" : "text/html"});
        res.write("I am USER/*");
        res.end();
    // });

    console.log("Request handler 'user' process end.");
}

function __NotFound(res){
    console.log("Request handler was NOT FOUND.");

    res.writeHead("200", {"Content-Type" : "text/html"});
    res.write("404 Not Found.");
    res.end();

    console.log("Request handler '__NotFound' process end.");
}

exports.index = index;
exports.user = user;
exports.__NotFound = __NotFound;